package com.qiyue.admin.controller.base;

import com.qiyue.admin.biz.base.ElementBiz;
import com.qiyue.admin.biz.base.MenuBiz;
import com.qiyue.admin.biz.base.UserBiz;
import com.qiyue.admin.entity.base.Element;

import com.qiyue.common.controller.BaseController;
import com.qiyue.utils.ResultUtils;
import com.qiyue.vo.Result;
import com.qiyue.vo.TableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.ws.rs.GET;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by y747718944 on 2018/2/8
 */
@RestController
@RequestMapping("element")
@Api(description = "ElementController")
public class ElementController  extends BaseController<ElementBiz,Element> {

    @Autowired
    private UserBiz userBiz;
    @Autowired
    private MenuBiz menuBiz;


//    @Override
//    public Result save(Element element) {
//        element.toString();
//        return super.save(element);
//    }
//
//    @Override
//    public Result update(Element element) {
//        return super.update(element);
//    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public TableResult<Element> page(@RequestParam(defaultValue = "10") int limit,
                                     @RequestParam(defaultValue = "1") int offset,
                                     String name,
                                     @RequestParam(defaultValue = "0") int menuId) {

        Map<String,Object> filters = new HashMap<String,Object>();
        filters.put("EQ_menu.id",menuId);
        if(StringUtils.isNotBlank(name)){
            filters.put("LIKE_name",name);
        }
        List<Element> elements = baseBiz.findList(filters);
        return new TableResult(elements.size() , elements);
    }

    @GetMapping("/user")
    @ApiOperation("根据当前用户查询 是否拥有改菜单权限")
    public Result<Element> getAuthorityElement(String menuId){
        Long userId =  userBiz.getUserByAccout(getCurrentUserAccount()).getId();
        List<Element> elements = baseBiz.getAuthorityElementByUserIdAndMenuId(userId+"" ,menuId);
        return ResultUtils.getResult(elements);

    }

    @GetMapping("/user/menu")
    @ApiOperation("根据当前用户 获取菜单列表")
    public Result<Element> getAuthorityElement(){
        Long userId =  userBiz.getUserByAccout(getCurrentUserAccount()).getId();
        List<Element> elements = baseBiz.getAuthorityElementByUserId(userId+"");
        return ResultUtils.getResult(elements);
    }

}
